package com.qf.rabbitmq.route;

import com.qf.rabbitmq.pojo.User;
import com.qf.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.concurrent.TimeoutException;

public class Sender {
    private static final String EXCHANGENAME="bk2102EXCHANGE";
    public static void main(String[] args) throws IOException, TimeoutException {
//        1 获取连接
        Connection connection = ConnectionUtil.getConnection();
//        2创建通道
        Channel channel = connection.createChannel();
//        3 声明交换机
        channel.exchangeDeclare(EXCHANGENAME, "direct");
//        4 发送消息  此次发送消息需要指定routingkey
        User user=new User();

        for (int i = 0; i < 10; i++) {

            user.setName("张三");
            user.setSex("男");

            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(user);
            byte[] bytes = byteArrayOutputStream.toByteArray();

//                              交换机             指定的routingkey
            channel.basicPublish(EXCHANGENAME,"shuijiao", null,bytes);
            channel.basicPublish(EXCHANGENAME,"haha", null,bytes);
        }
        channel.close();
        connection.close();
    }
}
